package com.huobao.yunying.activitys_store;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Store_shangpin_list;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.StoreProgect;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.yunying.views.PageGridView;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class Act_Store_Progect_List extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.gridview)
	private PageGridView gridView;
	private Adapter_Store_shangpin_list adapter;
	private int m_CurrentPage = 1;
	/**
	 * 店铺id
	 */
	private String storeId;
	/**
	 * 分类id
	 */
	private String cate_id;
	/**
	 * 关键词
	 */
	private String keyword;
	private boolean isLoading = false;

	@Override
	protected int getContentView() {
		return R.layout.layout_store_progect_list;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		gridView.setOnItemClickListener(this);
		gridView.setNumColumns(2);
		gridView.setVerticalSpacing(10);
		gridView.setHorizontalSpacing(15);
		// 这里注意，一定要设置Gravity为Gravity.CENTER,否则不会出现页脚
		gridView.setGravity(Gravity.CENTER_HORIZONTAL);
		View footview = LayoutInflater.from(context).inflate(R.layout.item_footview, null);
		gridView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					// 滚动到底,请求下一页数据
					if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
						if (isLoading)
							return;
						m_CurrentPage++;
						getData(false);
					}
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (view.getLastVisiblePosition() != (view.getCount() - 1)) {
					// gridView.updateFooter(View.GONE);
				}
			}
		});
		adapter = new Adapter_Store_shangpin_list(context);
		gridView.setAdapter(adapter);
		getIntentData();
		getData(true);
	}

	private void getIntentData() {
		Intent intent = getIntent();
		storeId = intent.getStringExtra("id");
		cate_id = intent.getStringExtra("cate_id");
		keyword = intent.getStringExtra("keyword");
	}

	private void getData(boolean isshowDialog) {
		isLoading = true;
		gridView.updateFooter(View.VISIBLE);
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "store", HttpConst.SECOND_KEY, "goods", "id",
				storeId, "cate_id", cate_id, "keyword", keyword, "price", "", "page", m_CurrentPage + "");
		Http.getInstant().sendRequest(context, isshowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String s_title = jsonObject.getString("s_title");
				List<StoreProgect> list = JSON.parseArray(jsonObject.getString("goods_list"), StoreProgect.class);
				adapter.addAll(list);
				mAbTitleBar.setTitleText(s_title);
				gridView.updateFooter(View.GONE);
				if (list == null || list.size() == 0) {
				}else{
					isLoading = false;
				}
			}

			@Override
			public void onHttpError(String error) {
				isLoading = false;
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String goods_id = adapter.getItem(position).goods_id;
		SkipUtils.seeDetail(context, goods_id, adapter.getItem(position).default_image);
	}
}
